﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rInvoice.Utils;

namespace rInvoice
{
    public partial class UsersInputPanel : Form
    {
        private ServerObject mServerObject = null;
        int mUserID = 0;
        string mLoghin = string.Empty;
        string mFirstName = string.Empty;
        string mLastName = string.Empty;
        int mRole = 0;
        string mPassword = string.Empty;
        int mRecordStatus = 0;
        int mPasswordStatus = 0;
        bool mCreatingNew = false;
        bool mIsLoading = false;

        public int UserID
        {
            get { return mUserID; }
            set { mUserID = value; }
        }

        public string FirstName
        {
            get { return mFirstName; }
            set { mFirstName = value; }
        }

        public string LastName
        {
            get { return mLastName; }
            set { mLastName = value; }
        }
        
        public string Loghin
        {
            get { return mLoghin; }
            set { mLoghin = value; }
        }

      
        public int Role
        {
            get { return mRole; }
            set { mRole = value; }
        }

        public int RecordStatus
        {
            get { return mRecordStatus; }
            set { mRecordStatus = value; }
        }

        public int PasswordStatus
        {
            get { return mPasswordStatus; }
            set { mPasswordStatus = value; }
        }

        public string Password
        {
            get { return mPassword; }
            set { mPassword = value; }
        }

        public bool CreateNew
        {
            get
            {
                return mCreatingNew;
            }

            set
            {
                mCreatingNew = value;
            }
        }

        public UsersInputPanel()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void UsersInputPanel_Load(object sender, EventArgs e)
        {
            mIsLoading = true;

            firstNameTextBox.Text = mFirstName;
            lastNameTextBox.Text = mLastName;
            loginTextBox.Text = mLoghin;
            passwordTextBox.Text = mPassword;
            passwordConfirmTextBox.Text = mPassword;
            rolesComboBox.SelectedValue = mRole;
            loginTextBox.Enabled = mCreatingNew;

            FillRoleComboBox();

            if (mPasswordStatus == 1)
            {
                setPasswordCheckBox.Checked = true;
            }
            else
            {
                setPasswordCheckBox.Checked = false;
            }

            if (mRecordStatus == 1)
            {
                activeUserCheckBox.Checked = true;
            }
            else
            {
                activeUserCheckBox.Checked = false;
            }
            mIsLoading = false;
            
        }

        private void FillRoleComboBox()
        {
            DataTable gr1 = mServerObject.GetClassifiersListByTypeID(UFT.Table_TypeID.NAME_RoleType);
            gr1.Rows.Add("0", "**", "");
            if (gr1.Rows.Count-1 > 0)
            {                   
                rolesComboBox.DataSource = gr1;
                rolesComboBox.DisplayMember = "Name";
                rolesComboBox.ValueMember = "CodeID";
                if (mRole > 0)
                {
                    rolesComboBox.SelectedValue = mRole;
                }
                else
                {
                    rolesComboBox.SelectedValue = 0;
                }
            }
            else
            {
                rolesComboBox.Text = "For this classifier enter the required data.";
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {          
            SetUsers();
        }

        private void SetUsers()
        {
            bool result = false;
            bool resultDouble = mServerObject.GetDoubleCheckUsers(mLoghin);
           
                if (mCreatingNew)
                {
                    if (resultDouble)
                    {

                        result = mServerObject.AddUsers(mFirstName, mLastName, mLoghin, mPassword, mRole, mPasswordStatus, mRecordStatus);
                    }
                    else
                    {
                        MessageBox.Show("Sorry, but login is repeated!");
                    }
                }
                else
                {
                    result = mServerObject.UpdateUsers(mUserID, mFirstName, mLastName, mLoghin, mPassword, mRole, mPasswordStatus, mRecordStatus, 0);
                }

                if (result)
                {
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    this.DialogResult = DialogResult.Abort;
                }          
            
          
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Abort;
        }

        private void rolesComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading)
            {                
                mRole = (int)rolesComboBox.SelectedValue;
            }
        }

        private void loginTextBox_TextChanged(object sender, EventArgs e)
        {
            mLoghin = loginTextBox.Text.Trim();
        }

        private void firstNameTextBox_TextChanged(object sender, EventArgs e)
        {
            mFirstName = firstNameTextBox.Text.Trim();
        }

        private void lastNameTextBox_TextChanged(object sender, EventArgs e)
        {
            mLastName = lastNameTextBox.Text.Trim();
        }

        private void setPasswordCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (setPasswordCheckBox.Checked)
            {
                mPasswordStatus = 1;
            }
            else
            {
                mPasswordStatus = 0;
            }
        }

        private void passwordTextBox_TextChanged(object sender, EventArgs e)
        {
            mPassword = passwordTextBox.Text.Trim();
            CheckConfirmedPassword();
        }

        private void CheckConfirmedPassword()
        {
            string password = passwordTextBox.Text.Trim();
            string confirmedPassword = passwordConfirmTextBox.Text.Trim();
            if (password != confirmedPassword)
            {
                passwordTextBox.BackColor = Color.LightPink;
                passwordConfirmTextBox.BackColor = Color.LightPink;
            }
            else
            {
                passwordTextBox.BackColor = Color.White;
                passwordConfirmTextBox.BackColor = Color.White;
            }
        }

        private void passwordConfirmTextBox_TextChanged(object sender, EventArgs e)
        {
            CheckConfirmedPassword();
        }

        private void activeUserCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (activeUserCheckBox.Checked)
            {
                mRecordStatus = 1;
            }
            else
            {
                mRecordStatus = 0;
            }
        }
    }
}
